package com.bolt.auth.security.auth.server.service;


import com.bolt.auth.admin.api.sys.dto.UserInfo;
import com.bolt.auth.security.auth.server.model.OnlineUser;
import com.bolt.common.BoltConstants;
import com.bolt.common.utils.PageUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.common.web.RequestUtil;
import com.bolt.convention.data.PageInfo;
import com.bolt.support.cache.redis.RedisManager;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2020/9/17.
 */
@Service
@AllArgsConstructor
public class OnlineUserService {

    private final RedisManager redisManager;

    public void save(OnlineUser onlineUser, Serializable key, long expTime) {
        redisManager.cache(BoltConstants.ONLINEKEY + key, onlineUser, expTime, TimeUnit.SECONDS);
    }

    public void save(UserInfo userInfo, String token, long expTime, HttpServletRequest request){
        OnlineUser onlineUser = new OnlineUser();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        onlineUser.setIp(RequestUtil.getRemoteIpAddress(request));
        onlineUser.setBrowser(browser);
        onlineUser.setOs(os);
        onlineUser.setLoginTime(new Date());
        onlineUser.setUserId(userInfo.getUserId());
        onlineUser.setUserName(userInfo.getUserName());
        onlineUser.setOrgName(userInfo.getOrgName());
        onlineUser.setNickName(userInfo.getRealName());
        onlineUser.setKey(token);
        save(onlineUser,token,expTime);

    }

    /**
     * 退出登录
     *
     * @param token /
     */
    public void logOut(Serializable token) {
        String key = BoltConstants.ONLINEKEY + token;
        redisManager.del(key);
    }


    /**
     * 查询用户
     * @param token /
     * @return /
     */
    public OnlineUser getOne(Serializable token) {
        String key = BoltConstants.ONLINEKEY + token;
        return redisManager.get( key,OnlineUser.class);
    }


    public PageInfo<List<OnlineUser>> getAll(String userName, Pageable pageable){
        List<OnlineUser> onlineUsers = findAll(userName);
        return  PageUtil.toPage(pageable,onlineUsers);
    }

    public List <OnlineUser> findAll(String userName) {
        List <String> keys = redisManager.scan(BoltConstants.ONLINEKEY + "*");
        Collections.reverse(keys);
        List <OnlineUser> onlineUsers = new ArrayList <>();
        for (String key : keys) {
            OnlineUser onlineUser = redisManager.get(key, OnlineUser.class);
            if (StrUtil.isNotBlank(userName)) {
                if (onlineUser.getUserName().equals(userName)) {
                    onlineUsers.add(onlineUser);
                }
            } else {
                onlineUsers.add(onlineUser);
            }
        }
        return onlineUsers;
    }

}

 